<?php

class TestDbQueryTpl extends UnitTestCase {

    function testNumerics() {
        $tpl=new DbQueryTpl(
            'SELECT * FROM users WHERE id={{id}} AND secu={{secu}}');
        $this->assertEqual(
            $tpl->render(array('id'=>36,'secu'=>23456789))
            , "SELECT * FROM users WHERE id=36 AND secu=23456789"
            ,"numerics should be rendered as is");
        $tpl=new DbQueryTpl(
            'SELECT * FROM users WHERE 1{ AND num={num}}');
        $this->assertEqual(
            $tpl->render(array('num'=>1))
            , "SELECT * FROM users WHERE 1 AND num=1"
            ,"numerics should eval to true");
        $this->assertEqual(
            $tpl->render(array('num'=>0))
            , "SELECT * FROM users WHERE 1 AND num=0"
            ,"zero should eval to true");
    }

    function testStrings() {
        $tpl=new DbQueryTpl(
            'UPDATE users SET login={{login}} {, alias={alias}}');
        $this->assertEqual(
            $tpl->render(array('login'=>'toto','alias'=>'Maurice'))
            , "UPDATE users SET login='toto' , alias='Maurice'"
            ,"strings should be quoted");
        $this->assertEqual(
            $tpl->render(array('login'=>'toto','alias'=>''))
            , "UPDATE users SET login='toto' , alias=''"
            ,"empty strings should evaluate to true by default");
    }

    function testNulls() {
        $tpl=new DbQueryTpl(
            'UPDATE users SET login={{login}}{, alias={alias}}');
        $this->assertEqual(
            $tpl->render(array('login'=>'toto'))
            , "UPDATE users SET login='toto', alias=NULL"
            ,"by default, nulls should evaluate to true and render to NULL");
        $this->assertTrue($tpl->setNullBool(false)
            ,"setNullBool(bool) should return true");
        $this->assertEqual(
            $tpl->render(array('login'=>'toto'))
            , "UPDATE users SET login='toto'"
            ,"null evaluating may be set by setNullBool()");
    }

    function testEscaping() {
        $tpl=new DbQueryTpl(
            'SELECT * FROM users WHERE login={{login}}');
        $this->assertEqual(
            $tpl->render(array('login'=>"' OR 1'"))
            ,"SELECT * FROM users WHERE login='\' OR 1\''"
            ,"render() should use mysql_escape_strings by default on strings");
    }
}
